package com.wanwu.channel.core.imp;

import com.wanwu.channel.common.enums.ChannelPayStatus;
import com.wanwu.channel.common.enums.ExceptionEnum;
import com.wanwu.channel.common.exception.SystemException;
import com.wanwu.channel.core.ChannelPayService;
import com.wanwu.channel.core.vo.ChannelPayVo;
import com.wanwu.channel.dao.condition.BaseCondition;
import com.wanwu.channel.dao.datasource.DataSource;
import com.wanwu.channel.dao.datasource.DataSourceType;
import com.wanwu.channel.dao.entity.ChannelPayEntity;
import com.wanwu.channel.dao.inter.ChannelPayDao;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName ChannelPayServiceImpl
 * @Description 支付表操作相关服务实现
 * @Author husheng
 * @Date 2020-01-16 19:37
 */
@Component
public class ChannelPayServiceImpl implements ChannelPayService {

    @Resource
    private ChannelPayDao channelPayDao;

    /**
     * 校验或插入支付表
     * @param pay
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ChannelPayVo saveAndCheck(ChannelPayEntity pay) {
        ChannelPayVo channelPayVo = new ChannelPayVo();
        ChannelPayEntity dao = channelPayDao.queryByBizOrderNoFMaster(pay.getBizOrderNo(),pay.getBizCode());
        if (null == dao) {
            channelPayDao.insertSelective(pay);
            channelPayVo.setExist(Boolean.FALSE);
            channelPayVo.setChannelPayEntity(pay);
        } else {
            channelPayVo.setExist(Boolean.TRUE);
            channelPayVo.setChannelPayEntity(dao);
        }
        return channelPayVo;
    }

    /**
     * 根据渠道单号更新预支付流水
     * @param channelPayEntity
     */
    @Override
    public void updateByChannelNo(ChannelPayEntity channelPayEntity) {
        channelPayDao.updateByChannelNo(channelPayEntity);
    }

    /**
     * 更新订单终态
     * @param channelPayEntity
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ChannelPayEntity checkAndUpdateByChannelNo(ChannelPayEntity channelPayEntity) {
        ChannelPayEntity payInfo = channelPayDao.queryByBizOrderNoFMaster(channelPayEntity.getBizOrderNo(),channelPayEntity.getBizCode());
        channelPayEntity.setChannelNo(payInfo.getChannelNo());
        //只有数据库记录处于处理中状态以及从微信查到的状态不为处理中才更新
        if (Objects.isNull(payInfo)) {
            throw new SystemException(ExceptionEnum.R20006);
        }
        if (ChannelPayStatus.PROCESSING.getValue().equals(payInfo.getPayStatus()) &&
                !ChannelPayStatus.PROCESSING.getValue().equals(channelPayEntity.getPayStatus())) {
            channelPayEntity.setUpdateTime(new Date());
            channelPayDao.updateByChannelNo(channelPayEntity);
        }else{
            BeanUtils.copyProperties(payInfo, channelPayEntity);
        }
        return channelPayEntity;
    }

    @Override
    public Map selectCustomQryInfoByBizOrderNo(String bizOrderNo, String subOrderNo, String bizCode) {
        return channelPayDao.getCostomQryInfoByBizOrderNo(bizOrderNo, subOrderNo, bizCode);
    }

    @Override
    public ChannelPayEntity qryPayByBizOrderNo(String bizOrderNo,String bizCode) {
        return channelPayDao.queryByBizOrderNo(bizOrderNo,bizCode);
    }

    /**
     * 报关表列表查询
     * @Author hehuangtao
     * @Date 20:59 2020-03-24
     **/
    @Override
    @DataSource(DataSourceType.OFF_LINE)
    public List<ChannelPayEntity> qryPayList(ChannelPayEntity reqDto, BaseCondition condition){
        return channelPayDao.qryList(reqDto, condition);
    }
    @Override
    @DataSource(DataSourceType.OFF_LINE)
    public Integer countNum(ChannelPayEntity reqDto, BaseCondition condition){
        return channelPayDao.countNum(reqDto, condition);
    }

    @Override
    public ChannelPayEntity selectByOutTradeNoAndPayType(String outTradeNo, int payType) {
        return channelPayDao.selectByOutTradeNoAndPayType(outTradeNo, payType);
    }

    @Override
    public ChannelPayEntity queryByBizOrderNo(String outTradeNo,String bizCode) {
        return channelPayDao.queryByBizOrderNo(outTradeNo,bizCode);
    }

    @Override
    public ChannelPayEntity queryByBizOrderNoFMaster(String outTradeNo, String bizCode) {
        return channelPayDao.queryByBizOrderNoFMaster(outTradeNo,bizCode);
    }


    @Override
    @DataSource(DataSourceType.OFF_LINE)
    public List<ChannelPayEntity> qryMerchantNoEmptyList(Long id) {
        return channelPayDao.qryMerchantNoEmptyList(id);
    }

    @Override
    public ChannelPayEntity queryByChannelNoFMaster(String channelNo) {
        return channelPayDao.queryByChannelNoFMaster(channelNo);
    }
}

